package cn.tedu.hospital.admin.server.freebased.service.impl;


import cn.tedu.hospital.admin.server.common.ex.ServiceException;
import cn.tedu.hospital.admin.server.common.pojo.vo.PageData;
import cn.tedu.hospital.admin.server.common.web.ServiceCode;
import cn.tedu.hospital.admin.server.freebased.dao.persist.repository.IFreeBasedRepository;
import cn.tedu.hospital.admin.server.freebased.dao.persist.repository.IFreeBasedDetailRepository;
import cn.tedu.hospital.admin.server.freebased.pojo.entity.FreeBased;
import cn.tedu.hospital.admin.server.freebased.pojo.entity.FreeBasedDetail;
import cn.tedu.hospital.admin.server.freebased.pojo.param.FreeBasedAddNewParam;
import cn.tedu.hospital.admin.server.freebased.pojo.vo.FreeBasedListItemVO;
import cn.tedu.hospital.admin.server.freebased.pojo.vo.FreeBasedStandardVO;
import cn.tedu.hospital.admin.server.freebased.service.IFreeBasedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 处理患者数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@Service
public class FreeBasedServiceImpl implements IFreeBasedService {

    @Value("${tea-store.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IFreeBasedRepository freeBasedRepository;

    @Autowired
    private IFreeBasedDetailRepository freeBasedDetailRepository;


    public FreeBasedServiceImpl() {
        log.debug("创建业务类对象：ArticleServiceImpl");
    }

    @Override
    public void addNew(FreeBasedAddNewParam freeBasedAddNewParam) {
        log.debug("开始处理【添加消费信息】的业务，参数：{}",  freeBasedAddNewParam);

        // TODO 【检查类别是否存在】调用categoryRepository根据ID查询，如果结果为null则抛异常
        // TODO 【类别是否禁用】判断以上查询结果的enable，如果为0则抛异常
        // TODO 【检查是否非父级】判断以上查询结果的isParent，如果为1则抛异常

        FreeBased freeBased = new FreeBased();
        BeanUtils.copyProperties(freeBasedAddNewParam,freeBased);
        int rows = freeBasedRepository.insert(freeBased);
        if (rows != 1) {
            String message = "添加消费信息失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }

        FreeBasedDetail freeBasedDetail = new FreeBasedDetail();
        freeBasedDetail.setFreebasedId(freeBased.getFreebasedId());
        freeBasedDetail.setDetail(freeBasedAddNewParam.getDetail());
        rows = freeBasedDetailRepository.insert(freeBasedDetail);
        if (rows != 1) {
            String message = "添加消费信息详情失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除消费信息】的业务，参数：{}", id);
        FreeBasedStandardVO queryResult = freeBasedRepository.getStandardById(id);
        if (queryResult == null) {
            // 是：数据不存在，抛出异常
            String message = "删除消费信息失败，尝试删除的消费信息数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        int rows = freeBasedRepository.deleteById(id);
        if (rows != 1) {
            String message = "删除消费信息失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }

        rows = freeBasedDetailRepository.deleteByArticle(id);
        if (rows != 1) {
            String message = "删除消费信息失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }

    }


    @Override
    public FreeBasedStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询消费信息详情】的业务，参数：{}", id);
        FreeBasedStandardVO queryResult = freeBasedRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "查询消费信息详情失败，消费信息数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<FreeBasedListItemVO> list(Integer pageNum) {
        log.debug("开始处理【查询消费信息列表】的业务，页码：{}", pageNum);
        PageData<FreeBasedListItemVO> pageData = freeBasedRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<FreeBasedListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询消费信息列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<FreeBasedListItemVO> pageData = freeBasedRepository.list(pageNum, pageSize);
        return pageData;
    }


}